package com.example.mywf.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.mywf.R;
import com.example.mywf.bean.WeatherBean;

import java.util.List;
import java.util.regex.Pattern;

public class ForecastAdapter extends ArrayAdapter<WeatherBean.DataDTO.ForecastDTO> {
    private int newResourceId;
    public ForecastAdapter(@NonNull Context context, int resource, @NonNull List<WeatherBean.DataDTO.ForecastDTO> objects) {
        super(context, resource, objects);
        newResourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        WeatherBean.DataDTO.ForecastDTO detail = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(newResourceId,parent,false);

        TextView week = view.findViewById(R.id.week_tv);
        TextView type = view.findViewById(R.id.type_tv);
        TextView high = view.findViewById(R.id.high_tv);
        TextView low = view.findViewById(R.id.low_tv);
        String REGEX = "[^(0-9)]";
        String highString = Pattern.compile(REGEX).matcher(detail.getHigh()).replaceAll("").trim();
        String lowString =  Pattern.compile(REGEX).matcher(detail.getLow()).replaceAll("").trim();
        week.setText(detail.getWeek());
        type.setText(detail.getType());
        high.setText(highString);
        low.setText(lowString);

        return view;
    }
}
